//
//  JCMomentMessageListVC.m
//  Victor
//
//  Created by 郭吉成 on 2017/10/13.
//  Copyright © 2017年 coollang. All rights reserved.
//

#import "JCMomentMessageListVC.h"
#import "JCMomentMessageCell.h"
#import "WSProgressHUD.h"
#import "MJRefresh.h"
#import "JCMessageListModel.h"
#import "YFMomentApi.h"
#import "UIScrollView+YFMJRefresh.h"

@interface JCMomentMessageListVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *listArray;
@property (assign, nonatomic) NSInteger pageIndex;
@end

@implementation JCMomentMessageListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCode];
    [self loadData];
}

- (void)initCode{
    self.navigationItem.title = NSLocalizedString(@"News", @"消息");
    _listArray = [NSMutableArray array];
    
    __weak typeof(self)weakSelf = self;
    [_tableView addLegendHeaderWithShowTime:NO RefreshingBlock:^{
        [weakSelf loadData];
    }];
    [_tableView addNormalFooterWithRefreshingBlock:^{
        [weakSelf loadMore];
        
    }];
}

/**
 加载数据
 */
- (void)loadData{
    __weak typeof(self)weakSelf = self;
    [self.hud showWithMaskType:WSProgressHUDMaskTypeClear];
    _pageIndex = 1;
    
    YFMomentApi *messageApi = [YFMomentApi messageApiWithApiType:kMomentGetMessageList argument:@{@"page":@(_pageIndex)}];
    [[YFNetworkingService sharedInstance] sendRequest:messageApi callback:^(BOOL success, id result, NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        if (error) {
            [weakSelf.hud showString:error.domain dimissAfterSecond:1];
        }else{
            [weakSelf.hud dismiss];
            NSArray *list = [JCMessageListModel creatMessageListWithDictionnary:[result responseObject]];
            weakSelf.listArray = list.mutableCopy;
            [weakSelf.tableView reloadData];
        }
    }];
}

/**
 上拉加载更多数据
 */
- (void)loadMore{
    __weak typeof(self)weakSelf = self;
    _pageIndex++;
    YFMomentApi *messageApi = [YFMomentApi messageApiWithApiType:kMomentGetMessageList argument:@{@"page":@(_pageIndex)}];
    [[YFNetworkingService sharedInstance] sendRequest:messageApi callback:^(BOOL success, id result, NSError *error) {
        [weakSelf.tableView.mj_footer endRefreshing];
        if (error) {
            weakSelf.pageIndex -= 1;
            [weakSelf.hud showErrorWithString:error.domain dimissAfterSecond:1];
        }else{
            [weakSelf.hud dismiss];
            NSArray *messageList = [JCMessageListModel creatMessageListWithDictionnary:[result responseObject]];
            if (messageList.count == 0) {
                weakSelf.pageIndex -= 1;
                [weakSelf.hud showString:NSLocalizedString(@"No more data", @"没有更多数据了") dimissAfterSecond:1];
            }else{
                for (JCMessageListModel *model in messageList) {
                    [weakSelf.listArray addObject:model];
                }
                [weakSelf.tableView reloadData];
            }
        }
    }];
}

/**
 删除
 */
- (IBAction)deleteAction:(UIBarButtonItem *)sender {
    if (sender.tag) {
        sender.tag = 0;
        [_tableView setEditing:NO animated:YES];
    }else{
        sender.tag = 1;
        [_tableView setEditing:YES animated:YES];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JCMomentMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JCMomentMessageCell" forIndexPath:indexPath];
    [cell setMessageModel:_listArray[indexPath.row]];
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self)weakSelf = self;
    [self.hud showWithMaskType:WSProgressHUDMaskTypeClear];
    if(editingStyle == UITableViewCellEditingStyleDelete){
        JCMessageListModel *model = _listArray[indexPath.row];
        
        YFMomentApi *messageApi = [YFMomentApi messageApiWithApiType:kMomentDeleteMessage argument:@{@"ID":model.messageID}];
        
        [[YFNetworkingService sharedInstance] sendRequest:messageApi callback:^(BOOL success, id result, NSError *error) {
            if (error) {
                [weakSelf.hud showString:error.domain dimissAfterSecond:1];
            }else{
                [weakSelf.hud dismiss];
                [weakSelf.listArray removeObjectAtIndex:indexPath.row];
                [weakSelf.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                          withRowAnimation:UITableViewRowAnimationBottom];
            }
        }];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
